/**
 * 素材中心
 * */


(function () {
})();


//列表
function getFiles(){
    $.ajax({
        type: apis.erqiSucaiFiles.method,
        url: apis.erqiSucaiFiles.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                return
            }
            var imgs = res.data.imgs;
            var videos = res.data.videos;
            var h = '', i;
            //图片
            h = '';
            for (i = 0; i < imgs.length; i ++) {
                h += '<img src="' + imgs[i] + '"/>';
            }
            $('#imgShow').html(h);
            if (imgs.length > 0) {
                $('#isImg')[0].value = 1;
                $('#imgNum').html(imgs.length);
            }
            //视频
            h = '';
            for (i = 0; i < videos.length; i ++) {
                h += '<video width="320" height="240" class="videoContent" controls="" poster="">';
                h += '<source src="' + videos[i] + '" type="video/mp4">';
                h += '</video>';
            }
            $('#videoShow').html(h);
            if (videos.length > 0) {
                $('#isVideo')[0].value = 1;
                $('#videoNum').html(videos.length);
            }
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//列表
function getList(){
    var formData = {
        type: $('#type').val(),
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.erqiSucaiIndex.method,
        url: apis.erqiSucaiIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formArys = res.data;
            tbody.html(getHtml());
            $('#pageTotal').html(formArys.length);
            //pages.total = res.total;
            //pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//拼接字符串
function getHtml(){
    var d = formArys;
    var h = '', i, status;
    for (i = 0; i < d.length; i ++) {
        status = d[i].status;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].tname + '</td>';
        h += '<td>' + d[i].cnt + '</td>';
        h += '<td>';
        if (d[i].imglink == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].imglink + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>';
        if (d[i].vlink == '') {
            h += '-';
        } else {
            h += '<video width="300" height="240" class="videoContent" controls="" poster="">';
            h += '<source src="' + d[i].vlink + '" type="video/mp4">';
            h += '</video>';
        }
        h += '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="sucai_show(\'详情\',\'sucai-show.html\', ' + d[i].id + ');">';
        h += '详情';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="sucai_edit(\'编辑\',\'sucai-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="sucai_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//抓图
function uploadImg() {
    var fileObj = $('#imgUpload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var formData = {
            scid: id, img: res.target.result,
        };
        //console.log(formData);
        //return
        $.ajax({
            type: apis.erqiSucaiImgUpload.method,
            url: apis.erqiSucaiImgUpload.url,
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 1,time: 1000});
                    return
                }
                //msgPop({msg: '上传成功'});
                layer.msg('上传成功',{icon: 1,time: 1000});
                setTimeout(function(){
                    //top.location.reload();
                    location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}


//视频上传
function uploadSp(){
    var uploadFile = $("#videoUpload")[0].files[0];
    var formData = new FormData();
    formData.append('scid', id);
    formData.append("video", uploadFile);
    $.ajax({
        type: apis.erqiSucaiVideoUpload.method,
        url: apis.erqiSucaiVideoUpload.url,
        data: formData,
        async: false,
        // 下面这两个参数
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            //msgPop({msg: '操作成功'});
            layer.msg('上传成功',{icon: 1,time: 1000});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//编辑
function sucai_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//编辑
function sucai_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(id){
    if (!id) {
        return
    }
    $.ajax({
        type: apis.erqiSucaiOne.method,
        url: apis.erqiSucaiOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                $('#gno')[0].value = '';
                $('#title')[0].value = '';
                $('#detail').html('');
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var imgs = res.data.imgs;
            var videos = res.data.videos;
            $('#cnt')[0].value = d.cnt;
            var h = '', i;
            //图片
            h = '';
            for (i = 0; i < imgs.length; i ++) {
                h += '<img src="' + imgs[i] + '"/>';
            }
            $('#imgShow').html(h);
            if (imgs.length > 0) {
                $('#isImg')[0].value = 1;
                $('#imgNum').html(imgs.length);
            }
            //视频
            h = '';
            for (i = 0; i < videos.length; i ++) {
                h += '<video width="320" height="240" class="videoContent" controls="" poster="">';
                h += '<source src="' + videos[i] + '" type="video/mp4">';
                h += '</video>';
            }
            $('#videoShow').html(h);
            if (videos.length > 0) {
                $('#isVideo')[0].value = 1;
                $('#videoNum').html(videos.length);
            }
            h = '';
            if (d.type == 1) {
                h += '<option value="1" selected>文字</option>';
                h += '<option value="2">图片</option>';
                h += '<option value="3">视频</option>';
            } else if (d.type == 2) {
                h += '<option value="1">文字</option>';
                h += '<option value="2" selected>图片</option>';
                h += '<option value="3">视频</option>';
            } else {
                h += '<option value="1">文字</option>';
                h += '<option value="2">图片</option>';
                h += '<option value="3" selected>视频</option>';
            }
            $('#type').html(h);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//获取详情
function showOne(id){
    $.ajax({
        type: apis.erqiSucaiOne.method,
        url: apis.erqiSucaiOne.url,
        data: { id: id, },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#cnt').html(d.cnt);
            var imgs = d.imgs, videos = d.videos;
            var h = '', i;
            //图片
            h = '';
            for (i = 0; i < imgs.length; i ++) {
                h += '<img src="' + imgs[i] + '"/>';
            }
            $('#imgShow').html(h);
            //视频
            h = '';
            for (i = 0; i < videos.length; i ++) {
                h += '<video class="videoContent" controls="" poster="" style="margin: 5px;">';
                h += '<source src="' + videos[i] + '" type="video/mp4">';
                h += '</video>';
            }
            $('#videoShow').html(h);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//显示主图
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).width(e.w).height(e.h);
}


//保存更新
function modify(){
    var isImg = $('#isImg').val();
    var isVideo = $('#isVideo').val();
    var cnt = $('#cnt').val();
    var formData = {
        id: id, cnt: cnt, isImg: isImg, isVideo: isVideo,
        type: $('#type').val(),
    };
    console.log(formData);
    //return
    if (cnt=='' && isImg==0 && isVideo==0) {
        msgPop({msg: '内容、图片或视频必须要有一个'}); return
    }
    $.ajax({
        type: apis.erqiSucaiModify.method,
        url: apis.erqiSucaiModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function sucai_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.erqiSucaiDel.method,
            url: apis.erqiSucaiDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}